//
//  FinderViewController.m
//  InSuger
//
//  Created by dllo on 15/11/27.
//  Copyright © 2015年 Dior_3g. All rights reserved.
//

#import "FinderViewController.h"
#import "FinderModel.h"
#import "HotModel.h"
#import "FinderCollectionViewCell.h"
#import "FinderDetailsViewController.h"
#import "BaseCollectionView.h"

@interface FinderViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic, strong) UICollectionView *collectionView;
@property(nonatomic, strong) NSMutableArray *arr;
@property(nonatomic, strong) MBProgressHUD *progress;
@end

@implementation FinderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self navigationControllerCreateData]; // navigationController
    [self collectionViewCreateData]; // collectionView的创建
    [self createData];// 请求数据
    
    self.progress = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    _progress.labelText = @"Loading...";
    
    _progress.mode = MBProgressHUDModeDeterminate;
}

- (void)navigationControllerCreateData
{
//    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    self.navigationItem.title = @"热门";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
}



- (void)createData{
    [AFN getdataWithString:@"http://api.liwushuo.com/v2/items?gender=1&generation=1&limit=50&offset=0" block:^(id block) {
        self.arr = [NSMutableArray array];
        NSDictionary *dic = block;
        NSArray *arr = dic[@"data"][@"items"];
        NSArray *array = [HotModel baseModelByArr:arr];

        for (HotModel *hot in array) {
//            FinderModel *finder = [[FinderModel alloc] init];
//            [finder setValuesForKeysWithDictionary:hot.data];
          FinderModel *finder = [FinderModel baseModelWithDic:hot.data];
            [self.arr addObject:finder];
        }
        [self.collectionView reloadData];
        [self.progress removeFromSuperview];
    }];
}

- (void)collectionViewCreateData {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake((WIDTH - 20) / 2, 2 * HEIGHT / 5);
    flowLayout.minimumInteritemSpacing = 3; // 行
    flowLayout.minimumLineSpacing = 8; // 列
    flowLayout.sectionInset = UIEdgeInsetsMake(8, 5, 0, 5);  // 给item设置边框  上左下右
    self.collectionView = [[BaseCollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowLayout backgroundColor:[UIColor colorWithWhite:0.9 alpha:0.7] superView:self.view showsHorizontalScrollIndicator:YES showsVerticalScrollIndicator:NO delegate:self];

    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
        make.size.mas_equalTo(self.view);
    }];
    
    [self.collectionView registerClass:[FinderCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
    @weakify(self);
    [self addColorChangedBlock:^{
        @strongify(self);
        self.collectionView.nightBackgroundColor = UIColorFromRGB(0x343434);
        self.navigationController.navigationBar.nightBarTintColor = UIColorFromRGB(0x444444);
        self.navigationController.navigationBar.normalBarTintColor = [UIColor redColor];
        
    }];

}

#pragma mark CollectionView Delegate Method
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.arr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FinderCollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    FinderModel *finder = self.arr[indexPath.row];
//    cell.picImageView.image = [UIImage imageNamed:@"PlaceHolder"];
    [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:finder.cover_image_url] placeholderImage:[UIImage imageNamed:@"PlaceHolder"]];
    cell.nameLabel.text = finder.name;
    cell.priceLabel.text = [NSString stringWithFormat:@"￥%@", finder.price];
    cell.favoriteLabel.text = [NSString stringWithFormat:@"♡%@",finder.favorites_count];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    FinderModel *finder = self.arr[indexPath.row];
    FinderDetailsViewController *fdVC = [[FinderDetailsViewController alloc] init];
    fdVC.str = finder.dataId;
    
    fdVC.hidesBottomBarWhenPushed = YES;// 6666 写在跳转前面
    
    [self.navigationController pushViewController:fdVC animated:YES];
}

//- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
//    
//    if (scrollView == self.collectionView) {
//        self.navigationController.hidesBarsOnSwipe = YES;
//    }
//    
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
